package com.kgc.ssm.mapper;

import com.kgc.ssm.entity.Orders;
import com.kgc.ssm.entity.OrdersExample;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.session.RowBounds;
@Mapper
public interface OrdersMapper {
    long countByExample(OrdersExample example);

    int deleteByExample(OrdersExample example);

    int deleteByPrimaryKey(Long id);

    int insert(Orders row);

    int insertSelective(Orders row);

    List<Orders> selectByExampleWithRowbounds(OrdersExample example, RowBounds rowBounds);

    List<Orders> selectByExample(OrdersExample example);

    Orders selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("row") Orders row, @Param("example") OrdersExample example);

    int updateByExample(@Param("row") Orders row, @Param("example") OrdersExample example);

    int updateByPrimaryKeySelective(Orders row);

    int updateByPrimaryKey(Orders row);

    @Select("select * from ORDERS o,QUOTE_DETAIL d,QUOTE Q " +
            "where o.id=d.ORDER_ID and d.QUOTE_ID=Q.ID " +
            "and Q.id=#{id}")
    List<Map<String,Object>> selectByIdOP(Integer id);

    @Update("update ORDERS set " +
            "UNIT_PRICE=#{map.UNIT_PRICE}, " +
            "AMOUNT=#{map.AMOUNT}, " +
            "END_DATE=#{map.END_DATE}, " +
            "START_DATE=#{map.START_DATE} " +
            "where ORDER_NUM=#{map.ORDER_NUM}")
    int updateByOrders(@Param("map") Map<String,Object> map);
    long selectLastId();

    @Select("SELECT o.*, i.status FROM ORDERS o JOIN ID_MAPPING i ON o.id = i.order_id " +
            "WHERE  (#{materialCode} IS NULL OR o.material_code LIKE CONCAT('%', #{materialCode}, '%') )" +
            "AND (#{materialName} IS NULL OR o.material_name LIKE CONCAT('%', #{materialName}, '%') )" +
            "AND (#{status} IS NULL OR i.status = #{status})")
    List<Map<String, Object>> findOrdersByConditions(@Param("materialCode") String materialCode,
                                                     @Param("materialName") String materialName,
                                                     @Param("status") String status,
                                                     RowBounds rowBounds);
    @Select("SELECT COUNT(*) FROM ORDERS o JOIN ID_MAPPING i ON o.id = i.order_id " +
            "WHERE  (#{materialCode} IS NULL OR o.material_code LIKE CONCAT('%', #{materialCode}, '%') )" +
            "AND (#{materialName} IS NULL OR o.material_name LIKE CONCAT('%', #{materialName}, '%') )" +
            "AND (#{status} IS NULL OR i.status = #{status})")
    Long countOrdersByConditions(@Param("materialCode") String materialCode,
                                 @Param("materialName") String materialName,
                                 @Param("status") String status);
    @Update({
            "<script>",
            "UPDATE ID_MAPPING",
            "SET status = #{status}",
            "WHERE order_id IN",
            "<foreach item='orderId' collection='orderIds' open='(' separator=',' close=')'>",
            "#{orderId}",
            "</foreach>",
            "</script>"
    })
    void updateOrderStatus(@Param("orderIds") List<Integer> orderIds, @Param("status") String status);
}